﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;
using System.Transactions;

namespace TestOnlineProject.Models.DataAccess
{
    public class UserTestDAO
    {
        private TestOnlineDBEntities db = new TestOnlineDBEntities();

        public List<UserTest> Select()
        {
            try
            {
                var userTests = db.UserTests.ToList();
                return userTests;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<int> SelectUserTestId()
        {
            try
            {
                var userTest = db.UserTests.Select(u => u.UserTestId).ToList();
                return userTest;
            }
            catch (Exception)
            {
                throw;
            }
        } 

        public List<UserTest> SelectByUserId(int userId)
        {
            try
            {
                var userTest = db.UserTests.Where(u => u.UserId == userId).ToList();
                return userTest;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<UserTest> SelectByTestId(int testId)
        {
            try
            {
                var userTests = db.UserTests.Where(t => t.TestId == testId).ToList();
                return userTests;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Insert(UserTest userTest)
        {
            try
            {
                db.UserTests.AddObject(userTest);
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public UserTest SelectByUserTestId(int userTestId)
        {
            try
            {
                var userTest = db.UserTests.Where(u => u.UserTestId == userTestId).FirstOrDefault();
                return userTest;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(UserTest userTest)
        {
            try
            {
                //var ut = db.UserTests.Where(u => u.UserTestId == userTest.UserTestId).FirstOrDefault();
                //ut.FinishTime = DateTime.Now;
                //ut.Mark = userTest.Mark;
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(UserTest userTest, string status)
        {
            try
            {
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public int DeleteByUserId(int userId)
        {
            try
            {
                var result = 0;
                var userTests = db.UserTests.Where(x => x.UserId == userId).ToList();
                foreach (var userTest in userTests)
                {
                    var userAnswers = db.UserAnswers.Where(x => x.UserTestId == userTest.UserTestId).ToList();
                    foreach (var userAnswer in userAnswers)
                    {
                        db.UserAnswers.DeleteObject(userAnswer);
                    }
                    db.UserTests.DeleteObject(userTest);
                    result += db.SaveChanges();
                }
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}